package com.zp.viewpractice;

import android.app.Application;
import android.content.res.Configuration;
import android.graphics.Point;
import android.util.DisplayMetrics;
import android.view.Display;
import android.view.WindowManager;


/**
 * Created by izp on 2017/7/28.
 */

public class FitApp extends Application {
    public final static float DESIGN_WIDTH = 375;//dp
    @Override
    public void onCreate() {
        super.onCreate();
        KLog.init(true,"vicky");
        logScreenInfo();
        fitScreen();
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        fitScreen();
    }
    public void fitScreen(){
        Point size = new Point();//用一个点来表示宽高，
        ((WindowManager)getSystemService(WINDOW_SERVICE)).getDefaultDisplay().getSize(size);
//        这样size中就保存了宽高信息
        getResources().getDisplayMetrics().xdpi = size.x/ DESIGN_WIDTH *72f;
    }
    private void logScreenInfo(){

        Display display = ((WindowManager)getSystemService(WINDOW_SERVICE)).getDefaultDisplay();
        Point size = new Point();
        display.getSize(size);
        DisplayMetrics displayMetrics = getResources().getDisplayMetrics();
//        基本数据：，比例、缩放比例，宽高px,每英寸像素
        KLog.v("基本数据：\n 屏幕密度"+displayMetrics.densityDpi+"\n 屏幕密度比："+displayMetrics.density+
                "\n缩放密度比："+displayMetrics.scaledDensity+
        "\n横向每英寸像素数量："+displayMetrics.xdpi+"\n竖向每英寸像素数量："+displayMetrics.ydpi+
        "\n横向宽度分辨率："+size.x+"\n纵向宽度分辨率："+size.y);
//        横英寸、毫米，竖英寸、毫米
        double xInch = size.x/displayMetrics.xdpi;
        double yInch = size.y/displayMetrics.ydpi;
        double xMm = xInch*25.4d;
        double yMm = yInch*24.4d;
//        对角线英寸
        double x2 = Math.pow(xInch,2);//计算次方，这里是二次方
        double y2 = Math.pow(yInch,2);
        double diagonalInch = Math.sqrt(x2+y2);//计算开方
        double diagonalMn = Math.sqrt(Math.pow(xMm,2)+Math.pow(yMm,2));//计算开方
        KLog.v("计算数据：\n横英寸："+xInch+"\n竖英寸："+yInch+"\n横毫米："+xMm+"\n竖毫米："+yMm+
                "\n对角线英寸："+diagonalInch+"\n对角线厘米："+diagonalMn);

    }
}
